/**
 * Copyright (c) 2015, 2016 itemis AG (http://www.itemis.eu) and others.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 * 
 * SPDX-License-Identifier: EPL-2.0
 */
package org.eclipse.xtend.core.tests.compiler;

import org.eclipse.xtend2.lib.StringConcatenation;
import org.junit.Test;

/**
 * @author Sebastian Zarnekow - Initial contribution and API
 */
@SuppressWarnings("all")
public class CompilerBug472142Test extends AbstractXtendCompilerTest {
  @Test
  public void test_01() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class C {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def boolean m(CharSequence obj) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("return obj?.subSequence(1,1) instanceof String && obj!==null && obj.getClass()!==null;");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("@SuppressWarnings(\"all\")");
    _builder_1.newLine();
    _builder_1.append("public class C {");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("public boolean m(final CharSequence obj) {");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("boolean _and = false;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("boolean _and_1 = false;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("CharSequence _subSequence = null;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("if (obj!=null) {");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("_subSequence=obj.subSequence(1, 1);");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("if (!(_subSequence instanceof String)) {");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("_and_1 = false;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("} else {");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("_and_1 = (obj != null);");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("if (!_and_1) {");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("_and = false;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("} else {");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("Class<? extends CharSequence> _class = obj.getClass();");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("boolean _tripleNotEquals = (_class != null);");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("_and = _tripleNotEquals;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("return _and;");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    this.assertCompilesTo(_builder, _builder_1);
  }

  @Test
  public void test_02() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class C {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def boolean m(CharSequence obj) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("return obj?.subSequence(1,1) instanceof String && obj!==null");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("@SuppressWarnings(\"all\")");
    _builder_1.newLine();
    _builder_1.append("public class C {");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("public boolean m(final CharSequence obj) {");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("boolean _and = false;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("CharSequence _subSequence = null;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("if (obj!=null) {");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("_subSequence=obj.subSequence(1, 1);");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("if (!(_subSequence instanceof String)) {");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("_and = false;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("} else {");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("_and = (obj != null);");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("return _and;");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    this.assertCompilesTo(_builder, _builder_1);
  }

  @Test
  public void test_03() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class C {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("var CharSequence mySeq");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def m() {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if(mySeq == null) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("mySeq = (all(\'s\', \'\'");
    _builder.append("\'s2\'");
    _builder.append("\'\').subList(1,1).head as String).ext");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("mySeq");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def java.util.List<CharSequence> all(String s, String s2) {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def ext(String s) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("s");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("import com.google.common.base.Objects;");
    _builder_1.newLine();
    _builder_1.append("import java.util.List;");
    _builder_1.newLine();
    _builder_1.append("import org.eclipse.xtend2.lib.StringConcatenation;");
    _builder_1.newLine();
    _builder_1.append("import org.eclipse.xtext.xbase.lib.IterableExtensions;");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@SuppressWarnings(\"all\")");
    _builder_1.newLine();
    _builder_1.append("public class C {");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("private CharSequence mySeq;");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("public CharSequence m() {");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("CharSequence _xblockexpression = null;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("{");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("boolean _equals = Objects.equal(this.mySeq, null);");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("if (_equals) {");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("StringConcatenation _builder = new StringConcatenation();");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("_builder.append(\"s2\");");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("CharSequence _head = IterableExtensions.<CharSequence>head(this.all(\"s\", _builder.toString()).subList(1, 1));");
    _builder_1.newLine();
    _builder_1.append("        ");
    _builder_1.append("this.mySeq = this.ext(((String) _head));");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("      ");
    _builder_1.append("_xblockexpression = this.mySeq;");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("return _xblockexpression;");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("public List<CharSequence> all(final String s, final String s2) {");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("return null;");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("public String ext(final String s) {");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("return s;");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    this.assertCompilesTo(_builder, _builder_1);
  }

  @Test
  public void test_04() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class C {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("var boolean b");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def m() {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("return b = (all(\'s\', \'\'");
    _builder.append("\'s2\'");
    _builder.append("\'\').subList(1,1).head instanceof String).ext");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def java.util.List<CharSequence> all(String s, String s2) {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def ext(boolean b) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("b");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("import java.util.List;");
    _builder_1.newLine();
    _builder_1.append("import org.eclipse.xtend2.lib.StringConcatenation;");
    _builder_1.newLine();
    _builder_1.append("import org.eclipse.xtext.xbase.lib.IterableExtensions;");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@SuppressWarnings(\"all\")");
    _builder_1.newLine();
    _builder_1.append("public class C {");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("private boolean b;");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("public boolean m() {");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("StringConcatenation _builder = new StringConcatenation();");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("_builder.append(\"s2\");");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("CharSequence _head = IterableExtensions.<CharSequence>head(this.all(\"s\", _builder.toString()).subList(1, 1));");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("return this.b = this.ext((_head instanceof String));");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("public List<CharSequence> all(final String s, final String s2) {");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("return null;");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("public boolean ext(final boolean b) {");
    _builder_1.newLine();
    _builder_1.append("    ");
    _builder_1.append("return b;");
    _builder_1.newLine();
    _builder_1.append("  ");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    this.assertCompilesTo(_builder, _builder_1);
  }
}
